var accountApp = angular.module('accountApp', ['ajoslin.promise-tracker']);

accountApp.controller('accountController' , function accountController($scope , $http , promiseTracker , $log , $timeout) {
    $scope.account = {};
    
    // process the form
    $scope.accountIndexProcess = function( form ) {
        $scope.submitted = true;
        if ($scope.accountForm.$invalid) { 
            var field = null,
            firstError = null;
            for (field in $scope.accountForm) {
                if (field[0] != '$')
                {
                    if (firstError === null && !$scope.accountForm[field].$valid) {
                        firstError = $scope.accountForm[field].$name;
                    }

                    if ($scope.accountForm[field].$pristine) {
                        $scope.accountForm[field].$dirty = true;
                    }
                }
            }        
            if(firstError != null){
                accountForm[firstError].focus();
            }             
            event.preventDefault();
            return;
        }
    }

});

accountApp.controller('accountAdressController' , function accountAdressController($scope , $http , promiseTracker , $log , $timeout) {
    $scope.textButton = 'Lưu lại';
    //$scope.accountAdress.city = true;
    //$scope.accountAdress.district = true;    
    $scope.cityChanged = function() {
        $http({
            method  : 'POST',
            url     : base_url + 'library/load_district_of_city' ,
            data    : $.param( { city: $scope.accountAdress.city } ) ,// { email: value } ,  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' } ,  // set the headers so angular passing info as form data (not request payload)

            } )
            .success(function(data) {                    
                //$scope.districts = data;
                //$scope.districts = data[0];
                //$scope.accountAdress.district = data[0];
               // $scope.accountAdress.district = data[0].code;
                //foreach
                var html = '';//<option value="">Quận/Huyện</option>';
                if(data != null){ console.log('data = ' , data);                    
                    angular.forEach(data, function(value) {      
                        if(value.id > 0){
                            html += '<option value="' + value.id + '">' + value.name + '</option>';
                        }
                    });                    
                }
                angular.element("#district").html( html );
            })
            .error(function(data) {
                
            });                   
    };
    // process the form
    
    $scope.focusErrorForm = function focusErrorForm( form , nameForm ){
        var field = null,
        firstError = null;
        for (field in form) { 
            if (field[0] != '$')
            {
                if (firstError === null && !form[field].$valid) {
                    firstError = form[field].$name;
                }

                if (form[field].$pristine) {
                    form[field].$dirty = true;
                }             
               
            }
        }                    
        if(firstError != null){
           nameForm[firstError].focus();
        }           
        event.preventDefault();
        return false;
    }
    $scope.accountAdressProcess = function( $event ) {         
        $scope.submitted = true;        
        if ($scope.accountAdressForm.$invalid) {             
            $scope.focusErrorForm($scope.accountAdressForm , accountAdressForm);   
        }
        return false;        
    }

});

/*
accountApp.directive('password_fail', ['$http', function($http) {  
  return {
    require: 'ngModel',
    link: function(scope, elem, attrs, ctrl) {
      scope.busy = false;
      scope.$watch(attrs.ngModel, function(value) {
        
        // hide old error messages
        ctrl.$setValidity('fail', true);
        //ctrl.$setValidity('invalidChars', true);
        
        if (!value) {
          // don't send undefined to the server during dirty check
          // empty username is caught by required directive
          return;
        } 
        
        scope.busy = true;
        
        $http({
            method  : 'POST',
            url     : base_url + 'user/check_password' ,
            data    : $.param( { email: value } ) ,// { email: value } ,  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' } ,  // set the headers so angular passing info as form data (not request payload)
           
            } )
            .success(function(data) {
                console.log('data = ' , data.isTaken);             
                // display new error message
                if (data.fail) {
                  ctrl.$setValidity('fail', false);
                } else if (data.invalidChars) {
                  //ctrl.$setValidity('invalidChars', false);
                }

                scope.busy = false;
            })
            .error(function(data) {
                 // display new error message
                if (data.isTaken) {
                  ctrl.$setValidity('fail', false);
                } else if (data.invalidChars) {
                 // ctrl.$setValidity('invalidChars', false);
                }
                scope.busy = false;          
            });        
      })
    }
  }
}]);
*/

accountApp.directive('match',['$parse', function ($parse) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ctrl) {
            //This part does the matching
            scope.$watch(function() {
                return (ctrl.$pristine && angular.isUndefined(ctrl.$modelValue)) || $parse(attrs.match)(scope) === ctrl.$modelValue;
            }, function(currentValue) {
                ctrl.$setValidity('match', currentValue);
            });

        },
    };
}]);

accountApp.controller('changepasswordController' , function changepasswordController($scope , $http , promiseTracker , $log , $timeout) {
    $scope.changePasswordProcess = function( $event ) {
        $scope.submitted = true;        
        if ($scope.changePasswordForm.$invalid) {             
            //$scope.focusErrorForm($scope.changePasswordForm , changePasswordForm);   
            event.preventDefault();
        }
        //event.preventDefault();
        //return;  
    }
});